package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.Banner;
import org.tea.inventory.db.domain.Contribute;
import org.tea.inventory.db.service.ContributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/contribute")	
public class ContributeController {

    @Autowired
    private ContributeService contributeService;

    /**
     * 获取banner信息列表
     */
    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId, String title,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       String sort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Contribute contribute = new Contribute();
        List<Contribute> lstContribute = contributeService.querySelective(contribute, page, limit, sort);

        int total = contributeService.countSelective(contribute);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", lstContribute);
        data.put("total", total);

        return ResponseUtil.ok(data);
    }

    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        Map contribute = contributeService.findLastContribute();

        return ResponseUtil.ok(contribute);
    }

    /**
     * 创建banner信息
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Contribute contribute) {

        if(adminId == null) 
            return ResponseUtil.unlogin();
        
        if(contribute == null )
        	return ResponseUtil.fail402();
        
        contribute.setCreatorId(adminId);
        contribute.setCreateDate(LocalDateTime.now());
        contributeService.add(contribute);

        return ResponseUtil.ok(contribute);
    }

    /**
     * 更新banner信息
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Contribute contribute) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        contributeService.update(contribute);

        return ResponseUtil.ok(contribute);
    }

    /**
     * 删除 banner 信息
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Banner banner) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(banner == null) {
            return ResponseUtil.badArgumentValue();
        }

        contributeService.delete(banner.getId());

        return ResponseUtil.ok();
    }
}
